const app = getApp()
import {
  ShareImg
} from "../../config/https";
//获取海报
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showShare: false,
    posterImg: '',        //获取的二维码
    shareImg: '',         //canvas生成的图片
    showSave: false
  },
  getShareImg() {
    ShareImg({
      token: wx.getStorageSync('token')
    }).then(res => {
      if (res.code == 1) {
        this.setData({
          posterImg: res.data
        })
      }
    })
  },
  showShare() {
    this.setData({
      showShare: true
    })
  },
  onClose() {
    this.setData({
      showShare: false
    })
  },
  showCreatePost() {
    let that = this
    let posterImg = this.data.posterImg
    this.setData({
      showShare: false,
      showSave: true
    })
    wx.showLoading({
      title: '二维码生成中..',
      mask: true
    })
    wx.getImageInfo({
      src: posterImg,
      success(res) {
        that.drawShareImg(res.path)
      },
      fail(err) {
        app.$tip('生成海报失败')
      }
    })
  },
  //绘画海报
  drawShareImg(img) {
    let that = this
    let title1 = '推荐好友拿佣金'
    let ctx = wx.createCanvasContext('myCanvas')
    ctx.drawImage('/assets/images/share_back.png', 0, 0, 260, 400);   //头部图片
    ctx.drawImage(img, 60, 120, 140, 140)      //二维码

    ctx.fillStyle = '#fff'
    ctx.font = 'bold 30px Arial, sans-serif'
    ctx.textAlign = 'center'
    ctx.fillText(title1, 130, 75);
    ctx.draw()

    //将画布转化成图片
    let timmm = setTimeout(() => {
      wx.canvasToTempFilePath({
        canvasId: "myCanvas",
        fileType: 'jpg',
        success(res) {
          wx.hideLoading();
          that.setData({
            shareImg: res.tempFilePath,
          })
          clearTimeout(timmm)
        },
        fail(err) {
          wx.hideLoading();
          app.$tip('生成错误，请重试！')
          console.log(err)
        }
      })
    }, 600)
  },
  // 关闭海报弹窗
  saveClose() {
    this.setData({
      showSave: false
    })
  },
  saveShareImg() {
    let img = this.data.shareImg
    wx.saveImageToPhotosAlbum({
      filePath: img,
      success: function (res) {
        wx.showToast({
          title: '保存成功'
        })
      },
      fail: function (res) {
        console.log(res)
        if (res.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
          wx.openSetting({
            success(settingdata) {
              console.log(settingdata)
              if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                app.$tip('获取权限成功，再次点击图片保存到相册')
              } else {
                app.$tip('获取权限失败')
              }
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getShareImg()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    this.setData({
      showShare: false
    })
    return {
      title: '邀请好友，共享好礼',
      path: '/pages/index/index?inviteCode=' + wx.getStorageSync('invitationCode')
    }
  }
})